<template>
	<view class="content">
		<image src="../../../../static/login_bg.png" style="width: 100%; height: 600rpx;"></image>
    <view class="auth-btn">
      <u-button type="primary" :loading="loadingFlag" open-type="getUserInfo" @getuserinfo="loginByWeiXin">微信授权登录</u-button>
      <view class="not-login">暂不登录</view>
    </view>
	</view>
</template>

<script>
	export default {
    onLoad(options) {
      if (options.turnBackPath && options.turnBackPath !== 'undefined') {
        this.turnBackPath = options.turnBackPath || ''
      }
    },
		data() {
			return {
				turnBackPath: '',
        loadingFlag: false
			}
		},
		methods: {
			loginByWeiXin(authResult) {
        this.loadingFlag = true
        let usrInfo = authResult.detail.userInfo;
        uni.login({
          success: (res) => {
            usrInfo['code'] = res.code
            uniCloud.callFunction({
              name: 'login-by-wei-xin',
              data: usrInfo
            }).then(res => {
              delete usrInfo.code
              usrInfo['uId'] = res.result.uid
              usrInfo['wxOpenId'] = res.result.openid
              uni.setStorageSync('uniIdToken', res.result.token)
              uni.setStorageSync(this.VUEX_USR_INFO_KEY, usrInfo)
              if (this.turnBackPath) {
                uni.redirectTo({
                  url: this.turnBackPath
                })
              } else {
                uni.navigateBack({
                  delta: 1
                })
              }
            })
          }
        })
      }
		}
	}
</script>

<style>
  .auth-btn {
    padding: 0 40rpx;
    margin-top: 110rpx;
  }
  .auth-btn .not-login {
    margin-top: 30rpx;
    color: #666666;
    text-align: center;
    text-decoration: underline;
  }
</style>
